﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using XCI.Extensions;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows;
using XCI.Windows.Extensions;
using XCI.Windows.Helper;

namespace XCI.Sys.Forms
{
    /// <summary>
    /// 系统应用管理
    /// </summary>
    [FormCaption("系统应用管理")]
    public partial class FrmAppManager : FrmSysBase
    {
        /// <summary>
        /// 表格操作类
        /// </summary>
        private GridHelper<SysApp> helper;
        private readonly bool allowSelect = SysRuntime.IsAuthorize("sys.app.select");
        private readonly bool allowInsert = SysRuntime.IsAuthorize("sys.app.insert");
        private readonly bool allowUpdate = SysRuntime.IsAuthorize("sys.app.update");
        private readonly bool allowDelete = SysRuntime.IsAuthorize("sys.app.delete");
        private readonly bool allowExport = SysRuntime.IsAuthorize("sys.app.export");

        public FrmAppManager()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        private void InitForm()
        {
            //1.初始化表格操作类
            helper = new GridHelper<SysApp>(gridView);
            helper.ElapsedTimeItem = labExecuteTime;

            //2.加载首页数据
            LoadData();

            ButtonStatus();
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            //权限控制
            if (!allowSelect) return;

            //1.获取查询条件
            var key = editKey.Text.Trim();
            var status = statusCombo.SelectedValue.ToBoolOrNull();

            helper.RunCallback(e =>
            {
                e.Result = AppService.Instance.SelectList(key, status);
            })
            .CompletCallback(e =>
            {
                ButtonStatus();
            })
            .RunAsync();
        }

        /// <summary>
        /// 数据编辑
        /// </summary>
        /// <param name="created">是否新建</param>
        /// <param name="model">模型对象</param>
        private void Edit(bool created, SysApp model = null)
        {
            var editForm = new FrmAppEdit();
            editForm.SetModel(created, model);
            editForm.ShowDialogOk(LoadData);
        }

        /// <summary>
        /// 获取选中的对象
        /// </summary>
        /// <returns>返回选中的对象</returns>
        private SysApp GetSelected()
        {
            return AppService.Instance.SelectById(helper.Selected.Id);
        }

        /// <summary>
        /// 设置按钮可用状态
        /// </summary>
        private void ButtonStatus()
        {
            btnNew.Enabled = allowInsert;
            btnEdit.Enabled = helper.HasSingleSelected && allowUpdate;
            btnCopy.Enabled = helper.HasSingleSelected && allowInsert;
            btnDelete.Enabled = helper.HasSelected && allowDelete;
            btnExport.Enabled = helper.HasData && allowExport;
            btnRefreshCache.Enabled = allowSelect;
            btnSearch.Enabled = allowSelect;
        }

        /// <summary>
        /// 页面加载事件
        /// </summary>
        private void FrmAppManager_Load(object sender, EventArgs e)
        {
            InitForm();
        }

        /// <summary>
        /// 表格双击事件
        /// </summary>
        private void gridView_DoubleClick(object sender, EventArgs e)
        {
            helper.DblClickRow(e, () =>
            {
                var detailsForm = new FrmAppDetails(helper);
                detailsForm.ShowDialogOk();
            });
        }

        /// <summary>
        /// 表格选中项发生变化事件
        /// </summary>
        private void gridView_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            ButtonStatus();
        }

        /// <summary>
        /// 关键字文本框键盘事件
        /// </summary>
        private void editName_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            //回车事件回调
            EnterPressHandle(e, LoadData);
        }

        /// <summary>
        /// 搜索按钮点击事件
        /// </summary>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            //加载首页数据
            LoadData();
        }

        /// <summary>
        /// 新建事件
        /// </summary>
        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit(true, AppService.Instance.New());
        }

        /// <summary>
        /// 修改事件
        /// </summary>
        private void btnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit(false, GetSelected());
        }

        /// <summary>
        /// 复制事件
        /// </summary>
        private void btnCopy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit(true, AppService.Instance.Copy(GetSelected()));
        }

        /// <summary>
        /// 删除事件
        /// </summary>
        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            helper.Delete(() =>
            {
                AppService.Instance.Delete(helper.SelectedIds).IfTrue(() =>
                {
                    gridView.DeleteSelectedRows();
                    LoadData();
                });
            });
        }

        private void BtnRefreshCache_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AppService.Instance.RefreshCache().IfTrue(() =>
            {
                LoadData();
                MessageBoxHelper.ShowMessage("服务器应用缓存刷新成功");
            });
        }

        private void BtnExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var key = editKey.Text.Trim();
            var status = statusCombo.SelectedValue.ToBoolOrNull();
            ExcelExportCore(() => AppService.Instance.Export(key, status), "系统应用");
        }
    }
}